package com.xiaofan.converter;
import lombok.SneakyThrows;
import org.springframework.core.convert.converter.*;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: 晓帆
 * @description:
 * @create: 2021-07-01 14:32
 **/
@Component
public class StringToDate implements Converter<String,Date>
{
    @SneakyThrows
    @Override
    public Date convert(String s) {
        SimpleDateFormat simpleDateFormat = null;
        if (s.length()==10){
             simpleDateFormat = new SimpleDateFormat("yy-MM-dd");
        }
        else if (s.length()==19){
           simpleDateFormat= new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        }
        Date parse = simpleDateFormat.parse(s);
        return parse;

    }
}
